package process

import (
	"encoding/json"
	"fmt"
	"strings"
	"testgin0012/chat/common"
)

type SmsProcess struct {
	Response *common.Response
}

func NewSmsProcess(response *common.Response) *SmsProcess {
	return &SmsProcess{
		Response: response,
	}
}

func (smsProcess SmsProcess) ResponseTest() {
	subMessage := common.ResponseTest{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	fmt.Println()
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	fmt.Print(subMessage.Content)
}
func (smsProcess SmsProcess) ResponseSayOneStart() {
	subMessage := common.ResponseSayOneStart{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	fmt.Println()
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	fmt.Printf(`"%v" 跟你说:%v`, subMessage.FromUser.NickName, subMessage.Content)
}
func (smsProcess SmsProcess) ResponseSayOne(User *common.User, SheUser *common.User, Blocked *bool) {
	subMessage := common.ResponseSayOne{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	fmt.Println()
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	if User.UserId == subMessage.FromUser.UserId {
		SheUser = subMessage.ToUser
		*Blocked = true
	}
	if User.UserId == subMessage.ToUser.UserId {
		SheUser = subMessage.FromUser
		fmt.Print(fmt.Sprintf(`用户 "%v" 正在给 "%v" 发消息:`, User.NickName, SheUser.NickName))
	}
}
func (smsProcess SmsProcess) ResponseSayAll() {
	subMessage := common.ResponseSayAll{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	fmt.Println()
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	fmt.Println()
	fmt.Printf(`用户"%v-%v": "%v"`, subMessage.NickName, subMessage.UserId, subMessage.Content)
	fmt.Println()
}
func (smsProcess SmsProcess) ResponseUserList() {
	subMessage := common.ResponseUserList{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	fmt.Println()
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	fmt.Println("用户名称\t用户昵称")
	for _, user := range subMessage.UserList {
		fmt.Printf("%v\t%v\n", user.UserId, user.NickName)
	}
}
func (smsProcess SmsProcess) ResponseString() {
	subMessage := common.ResponseString{}
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	fmt.Println("\n" + strings.Trim(subMessage.Content, "\n"))
}
func (smsProcess SmsProcess) ResponseLogin(UserId *int, NickName *string) {
	subMessage := common.ResponseLogin{}
	fmt.Println(smsProcess.Response.Data)
	err := json.Unmarshal([]byte(smsProcess.Response.Data), &subMessage)
	if err != nil {
		fmt.Printf("Message内层json.Unmarshal解码失败, err=%v\n", err)
		return
	}
	*UserId = subMessage.UserId
	*NickName = subMessage.NickName
	fmt.Println("\n登录成功")
}
